<?php
App::uses('AppModel', 'Model');
/**
 * Fee Model
 */
class BankReportTracking extends AppModel {
    public function save($data = null,$validate = true, $fieldList = array()) {
        $s = $this->sanitize($data);
        return parent::save($s);
    }

    function getLastReportRecord($bank_id,$type='internal'){
        $data  = $this->find('first',array('conditions'=>array('bank_id'=>$bank_id)));
        if($data){
            $last_trans_id = $data['BankReportTracking']['internal_last_trans_id'];
            if($type == 'external'){
                $last_trans_id = $data['BankReportTracking']['external_last_trans_id'];
            }
            return array(
                'id'=>$data['BankReportTracking']['id'],
                'last_trans_id'=>$last_trans_id
            );
        }
        else{
            //This means you are a new bank, so get the first transaction of type feepayment or service charge for this bank.
            $data = ClassRegistry::init('Transaction')->find('first',array(
                'fields'=>array('Transaction.id'),
                'conditions'=>array('Transaction.bank_id' => $bank_id),
                'recursive'=>-1
            ));
            if($data){
                $num = intval($data['Transaction']['id']) - 1; //We want to start from this record so subtract 1
                 return array(
                    'id'=>0,
                    'last_trans_id'=>$num
                );
            }
            else{//There is nothing yet to download
                return false;
            }
        }
    }


    function setLastTransId($id,$bank_id,$last_trans_id,$type='internal'){
        $save = array(
            'id' => $id,
            'bank_id'=>$bank_id,
            'internal_last_trans_id' => $last_trans_id
        );
        if($type == 'external'){
            $save = array(
                'id' => $id,
                'bank_id'=>$bank_id,
                'external_last_trans_id' => $last_trans_id
            );
        }
        $data = $this->save($save);
        if($data){
            return true;
        }
        else{
            return false;
        }
    }
}